import React, { useEffect, useState, useRef,memo } from 'react'
import {
    Modal, Form,
    Input,
     Button, message,Switch
} from 'antd';
import { post } from '../../util/post'
const  AddAddressModal=(props)=> {
    const { visible, setVisible,refresh } = props;
    const token = localStorage.getItem('TOKEN')
    const handleOk = () => {
        setVisible(false);
    };

    const handleCancel = () => {
        setVisible(false);
    };
    const onFinish = async (data) => {
        // console.log(data,'add')
        const {cdefault} = data
        data.cdefault = cdefault===undefined?false:true
        const res = await post('/user/address/save',data,token)
        if(res.data.code===0){
            message.success('成功')
            setVisible(false)
            if(refresh!==undefined) refresh(true)
        }
        else{
            message.error(res.data.msg)
        }
    }
    return (
        <div>
            <Modal title="新增地址信息"
            visible={visible}
            onOk={handleOk}
            onCancel={handleCancel}
            footer={null}
            >
            <Form
                labelCol={{ span: 4, }}
                wrapperCol={{ span: 14, }}
                layout="horizontal"
                onFinish={onFinish}
                // ref={ref}
            >
                <Form.Item
                    name="name"
                    label="姓名"
                    rules={[
                        {
                            required: true,
                            message: '请输入收货人姓名',
                        },
                    ]}
                >
                    <Input />
                </Form.Item>
                <Form.Item
                    name="phone"
                    label="手机号码"
                    rules={[
                        {
                            required: true,
                            message: '请输入手机号码',
                        },
                    ]}
                >
                    <Input />
                </Form.Item>
                <Form.Item
                    name="address"
                    label="地址"
                    rules={[
                        {
                            required: true,
                            message: '请输入地址',
                        },
                    ]}
                >
                    <Input />
                </Form.Item>
                <Form.Item
                    name="detail"
                    label="门牌号"
                >
                    <Input />
                </Form.Item>

                <Form.Item label="设为默用" name={'cdefault'} valuePropName="checked">
                        <Switch />
                    </Form.Item>
                <Form.Item
                    wrapperCol={{
                        offset: 8,
                        span: 16,
                    }}
                >
                    <Button type="primary" htmlType="submit">
                        提交
                    </Button>
                </Form.Item>
            </Form>
            </Modal>
        </div>
    )
}
export default React.memo(AddAddressModal)